﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SureKAM.BackUp.Notify.Presenter;
using SureKAM.BackUp.Notify.Presenter.Interfaces;
using SureKAM.BackUp.Notify.Presenter.ViewInterfaces;
using SureKAM.BackUp.Notify.Service;

namespace SureKAM.BackUp.Notify.WinForm
{
    public partial class NotifyWin : Form, INotifyWinView
    {
        #region 私有字段
        private INotifyWinPresenter iNotifyWinPresenter;
        #endregion

        #region 私有方法
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NotifyWin_Load(object sender, EventArgs e)
        {
            this.dataGridNotify.AutoGenerateColumns = false;
            this.iNotifyWinPresenter = new NotifyWinPresenter(this, new NotifyService());
            this.iNotifyWinPresenter.Init();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns>返回True成功 False失败</returns>
        private bool VaildateText()
        {
            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()) && !string.IsNullOrEmpty(this.txtEmail.Text.Trim()))
                return true;
            else
            {
                MessageBox.Show("请填写相关字段!");
                return false;
            }
        }

        /// <summary>
        /// 添加一个Notify对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_Click(object sender, EventArgs e)
        {
            if(this.VaildateText())
            {
                Domain.Notify notify=new Domain.Notify();
                notify.ID = Guid.NewGuid().ToString();
                notify.Name = this.txtName.Text.Trim();
                notify.Email = this.txtEmail.Text.Trim();
                notifyAdd(notify);
                this.ReDataBind();
                this.txtName.Text = "";
                this.txtEmail.Text = "";
            }
        }

        /// <summary>
        /// 重新绑定数据
        /// </summary>
        protected void ReDataBind()
        {
            this.dataGridNotify.DataSource = null;
            this.dataGridNotify.DataSource = reflush();
        }

        /// <summary>
        /// 改变GridView选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridNotify_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                this.txtEmail.Text = this.dataGridNotify.Rows[this.dataGridNotify.CurrentCell.RowIndex].Cells[2].Value.ToString();
                this.txtName.Text = this.dataGridNotify.Rows[this.dataGridNotify.CurrentCell.RowIndex].Cells[1].Value.ToString();
            }
            catch (Exception)
            {
             this.ReDataBind();  
            }
           
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            string currentId = this.dataGridNotify.Rows[this.dataGridNotify.CurrentCell.RowIndex].Cells[0].Value.ToString();
            if(notifyDelete(currentId)) this.ReDataBind();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void edit_Click(object sender, EventArgs e)
        {
            string currentId = this.dataGridNotify.Rows[this.dataGridNotify.CurrentCell.RowIndex].Cells[0].Value.ToString();
            if(!string.IsNullOrEmpty(currentId))
            {
                Domain.Notify notify=new Domain.Notify();
                notify.ID = currentId;
                notify.Name = this.txtName.Text;
                notify.Email = this.txtEmail.Text;
                if(notifyUpdate(notify)) this.ReDataBind();
            }
        }
        #endregion

        #region 属性
        public Reflush reflush { get; set; }
        public NotifyAdd notifyAdd { get; set; }
        public NotifyDelete notifyDelete { get; set; }
        public NotifyUpdate notifyUpdate { get; set; }
        #endregion

        #region 公有方法
        public NotifyWin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="notifies"></param>
        public void DataBindGrid(List<Domain.Notify> notifies)
        {
            if (notifies != null)
            {
                this.dataGridNotify.DataSource = null;
                this.dataGridNotify.DataSource = notifies;
            }
        }
        #endregion       

       
    }
}
